class GuaParticleSystem {
    constructor(game) {
        this.game = game
        this.setUp()
    }

    static new = function(game) {
        return new GuaParticleSystem(game)
    }

    setUp() {
        this.particles = []
        this.num_of_particles = 50

        this.x = 100
        this.y = 200
    }

    draw() {
        for (var i = 0; i < this.particles.length; i++) {
            var p = this.particles[i]
            p.draw()
        }
    }

    update() {
        if (this.particles.length < this.num_of_particles) {
            var p = Particle.new(this.game)
            var vx = randomBetween(-10, 10)
            var vy = randomBetween(-10, 10)
            p.init(this.x, this.y, vx, vy)
            this.particles.push(p)
        }

        for (var i = 0; i < this.particles.length; i++) {
            var p = this.particles[i]
            p.update()
        }

        this.particles = this.particles.filter(p => p.alive)
    }
}

class Particle {
    constructor(game) {
        this.game = game
        this.setUp()
    }
    static new = function(game) {
        var i = new this(game)
        return i
    }

    setUp() {
        this.image = GuaImage.new(this.game, 'particle')
        this.life = 10
        this.alive = true

    }

    fired() {
        if (this.life == 0) {
            this.alive = false
        }
        this.life--
    }

    init(x, y, vx, vy) {
        this.image.setPosition(x, y)
        this.vx = vx
        this.vy = vy
    }

    draw() {
        if (this.alive) {
            this.image.draw()
        }
    }

    update() {
        this.image.x += this.vx
        this.image.y += this.vy

        var factor = 0.03
        this.vx += factor * this.vx
        this.vy += factor * this.vy

        this.fired()
    }
}